<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索内容 -->
      <el-row>
        <el-col :span="6">
          <el-input
            placeholder="请输入搜索内容"
            v-model="queryInfo.query"
            @keyup.enter.native="getOrdersList"
            clearable
            @clear="getOrdersList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrdersList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表区域 -->
      <el-table :data="orderList" border stripe @expand-change="handleList">
        <el-table-column type="expand" label="详情" align="center">
          <template slot-scope="slots">
            <el-table
              ref="subTableData"
              :data="slots.row.children"
              tooltip-effect="light"
            >
              <el-table-column prop="portNumber" label="封面" align="center"></el-table-column>
              <el-table-column prop="name" label="商品名称" align="center"> </el-table-column>
              <el-table-column prop="ip" label="商品重量" align="center"> </el-table-column>
              <el-table-column prop="ip" label="商品价格(元)" align="center"></el-table-column>
              <el-table-column prop="ip" label="总价(元)" align="center"> </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号" align="center">
        </el-table-column>
        <el-table-column
          prop="order_number"
          label="订单编号"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="order_price"
          label="订单价格"
          align="center"
        ></el-table-column>
        <el-table-column label="是否付款" align="center">
          <!-- 自定义列模板，通过scope.row获取当前行的数据 -->
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status === '1'"
              >已付款</el-tag
            >
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="is_send"
          label="是否发货"
          align="center"
        ></el-table-column>
        <el-table-column label="下单时间" align="center" width="140">
          <template slot-scope="scope">{{
            scope.row.create_time | dateFormat
          }}</template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120">
          <template>
            <el-tooltip
              class="item"
              effect="dark"
              content="修改收货地址"
              placement="top"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="addressDialogVisible = true"
              ></el-button>
            </el-tooltip>

            <el-tooltip
              class="item"
              effect="dark"
              content="查看物流信息"
              placement="top"
            >
              <el-button
                type="success"
                icon="el-icon-location"
                size="mini"
                @click="showProgressDialog()"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
    <!-- 修改地址的对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressDialogVisible"
      width="50%"
      @close="addressDialogClosed"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            :options="cityData"
            v-model="addressForm.address1"
            class="w100"
          ></el-cascader>
        </el-form-item>

        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2" class="w100"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="address">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 展示物流进度的对话框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="progressDialogVisible"
      width="50%"
    >
      <!-- 时间线 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
          >{{ activity.context }}</el-timeline-item
        >
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from '../../assets/citydata.js'
export default {
  data() {
    return {
      // 获取订单用户列表的参数
      queryInfo: {
        query: '',
        pagenum: 1, //页码
        pagesize: 5, //页大小
      },
      orderList: [],
      // 总条数
      total: 0,
      // 修改地址对话框的显示和隐藏
      addressDialogVisible: false,
      addressForm: {
        address1: [],
        address2: '',
      },
      addressFormRules: {
        address1: { required: true, message: '请选择', trigger: 'blur' },
        address2: {
          required: true,
          message: '请填写详细地址',
          trigger: 'blur',
        },
      },
      cityData,
      // 物流进度对话框的显示和隐藏
      progressDialogVisible: false,
      progressInfo: [],
    }
  },
  created() {
    // 获取订单列表数据
    this.getOrdersList()
  },
  methods: {
    // 获取订单列表
    async getOrdersList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败')
      }
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 获取订单详情
    async handleList(row){
        const { data: res } = await this.$http.get(`orders/${row.order_id}`)
        console.log(res)
    },
    // 处理分页大小
    handleSizeChange(pagesize) {
      this.queryInfo.pagesize = pagesize
      // 刷新订单列表
      this.getOrdersList()
    },
    handleCurrentChange(pagenum) {
      this.queryInfo.pagenum = pagenum
      this.getOrdersList()
    },
    // 监听修改地址的对话框的关闭
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
    },
    address() {
      this.addressDialogVisible = false
    },
    async showProgressDialog() {
      this.$message.error('请求失败')
      const { data: res } = await this.$http.get('/kuaidi/804909574412544580')
      if (res.meta.status !== 200) {
        return this.$message.error('请求失败')
      }
      this.progressInfo = res.data
      this.$message.success('获取物流进度成功')
      this.progressDialogVisible = true
    },
  },
}
</script>

<style lang="less" scoped></style>
